package crmdna.inventory;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import crmdna.common.UnitUtils.PhysicalQuantity;
import crmdna.common.UnitUtils.ReportingUnit;

public class InventoryItemProp implements Comparable<InventoryItemProp> {
	public long inventoryItemId;

	public long groupId;
	public long inventoryItemTypeId;

	public String displayName;
	public String name;
	public PhysicalQuantity physicalQuantity;
	public ReportingUnit reportingUnit;

	public double quantityInDefaultUnit;

	// dependents
	public String inventoryItemType;
	public double quantityInReportingUnit;

	
	public static void populateDependents(String client,
			Iterable<InventoryItemProp> props) {

		Set<Long> inventoryItemTypeIds = new HashSet<>();
		for (InventoryItemProp prop : props) {
			inventoryItemTypeIds.add(prop.inventoryItemTypeId);
		}

		Map<Long, InventoryItemTypeEntity> map = InventoryItemType.get(client,
				inventoryItemTypeIds);		

		for (InventoryItemProp prop : props) {
			if (map.containsKey(prop.inventoryItemTypeId)) {
				prop.inventoryItemType = map.get(prop.inventoryItemTypeId).displayName;
			}			
		}
	}

	@Override
	public int compareTo(InventoryItemProp o) {
		if ((inventoryItemType == null) || (o.inventoryItemType == null))
			return 0;

		if (inventoryItemType.equals(o.inventoryItemType))
			return displayName.toLowerCase().compareTo(
					o.displayName.toLowerCase());

		return inventoryItemType.compareTo(o.inventoryItemType);
	}
}
